* Table A8: Intergenerational Mobility in the 1948-1953 and 1961-1964 Cohorts Using Full Samples
clear
use "${data_dir}DavisMazumderData.dta"

keep if age_firstSurvey<=18
drop if miss0 | miss1
drop if (mom_outOfRange==1 & linkMom==1 & !(dad_outOfRange==0 & linkDad==1)) | (dad_outOfRange==1 & linkDad==1 & !(mom_outOfRange==0 & linkMom==1)) | (linkMom==0 & linkDad==0) 
 
 
 
* Generate income ranks after applying sample restrictions
gen N = .
 tab n  if surv79==0 & women==0  [w=weight]
 replace N = r(N) if surv79==0 & women==0 
 tab n  if surv79==0 & women==1   [w=weight]
 replace N = r(N) if surv79==0 & women==1 
 tab n  if surv79==1 & women==0   [w=weight]
 replace N = r(N) if surv79==1 & women==0 
 tab n  if surv79==1 & women==1 [w=weight]
 replace N = r(N) if surv79==1 & women==1 
 
* Family Income Rank in Parent Generation
set sortseed 6688571
sort women surv79 faminc0
by women surv79: gen __rank0 = sum(weight) 
gen _rank0 = __rank0/N 
by women surv79 faminc0: egen rank0 = mean(_rank0) 
 replace rank0 = 100*rank0
gen perc66 = rank0*(1-surv79)
gen perc79 = rank0*surv79

 
* Family Income Rank in Kid Generation
set sortseed 6688571
sort women surv79 faminc1
by women surv79: gen __rank1 = sum(weight)
gen _rank1 = __rank1/N 
bys women surv79 faminc1: egen rank1 = mean(_rank1) 
replace rank1 = 100*rank1

drop N __rank0 _rank0 __rank1 _rank1  

 gen N_early = .
 tab n  if surv79==0 & women==0 & miss0==0  & miss1_early==0 [w=weight]
 replace N_early = r(N) if surv79==0 & women==0 & miss0==0  & miss1_early==0
 tab n  if surv79==0 & women==1  & miss0==0 &  miss1_early==0 [w=weight]
 replace N_early = r(N) if surv79==0 & women==1 & miss0==0  & miss1_early==0
 tab n  if surv79==1 & women==0  & miss0==0 &  miss1_early==0 [w=weight]
 replace N_early = r(N) if surv79==1 & women==0 & miss0==0 & miss1_early==0
 tab n  if surv79==1 & women==1 & miss0==0 &  miss1_early==0 [w=weight]
 replace N_early = r(N) if surv79==1 & women==1 & miss0==0   & miss1_early==0
 
 
gen N_prime = .
 tab n  if surv79==0 & women==0 & miss0==0  & miss1_prime==0 [w=weight]
 replace N_prime = r(N) if surv79==0 & women==0 & miss0==0  & miss1_prime==0
 tab n  if surv79==0 & women==1  & miss0==0 &  miss1_prime==0 [w=weight]
 replace N_prime = r(N) if surv79==0 & women==1 & miss0==0  & miss1_prime==0
 tab n  if surv79==1 & women==0  & miss0==0 &  miss1_prime==0 [w=weight]
 replace N_prime = r(N) if surv79==1 & women==0 & miss0==0 & miss1_prime==0
 tab n  if surv79==1 & women==1 & miss0==0 &  miss1_prime==0 [w=weight]
 replace N_prime = r(N) if surv79==1 & women==1 & miss0==0   & miss1_prime==0 
 
 
* Family Income Rank in Parent Generation (Early Income)
gen faminc0_nm_early = faminc0
replace faminc0_nm_early = . if !(miss0==0 & miss1_early==0)
set sortseed 6688571
sort women surv79 faminc0_nm_early
by women surv79: gen __rank0_early = sum(weight) if  miss0==0 & miss1_early==0
gen _rank0_early = __rank0_early/N_early if  miss0==0 & miss1_early==0
by women surv79 faminc0_nm_early: egen rank0_early = mean(_rank0_early) if  miss0==0 & miss1_early==0
 replace rank0_early = 100*rank0_early
drop faminc0_nm_early __rank0_early __rank0_early 
 
* Family Income Rank in Kid Generation (Early Income)
gen faminc1_nm_early = faminc1_early
 replace faminc1_nm_early = . if !(miss0==0 & miss1_early==0)
set sortseed 6688571 
sort women surv79 faminc1_nm_early
by women surv79: gen __rank1_early = sum(weight) if  miss0==0 & miss1_early==0
gen _rank1_early = __rank1_early/N_early if  miss0==0 & miss1_early==0
bys women surv79 faminc1_nm_early: egen rank1_early = mean(_rank1_early) if  miss0==0 & miss1_early==0
replace rank1_early = 100*rank1_early


* Family Income Rank in Parent Generation (Prime Income)
gen faminc0_nm_prime = faminc0
replace faminc0_nm_prime = . if !(miss0==0 & miss1_prime==0)
set sortseed 6688571
sort women surv79 faminc0_nm_prime
by women surv79: gen __rank0_prime = sum(weight) if  miss0==0 & miss1_prime==0
gen _rank0_prime = __rank0_prime/N_prime if  miss0==0 & miss1_prime==0
by women surv79 faminc0_nm_prime: egen rank0_prime = mean(_rank0_prime) if  miss0==0 & miss1_prime==0
 replace rank0_prime = 100*rank0_prime
drop faminc0_nm_prime __rank0_prime __rank0_prime 
 
* Family Income Rank in Kid Generation (Prime Income)
gen faminc1_nm_prime = faminc1_prime
 replace faminc1_nm_prime = . if !(miss0==0 & miss1_prime==0)
set sortseed 6688571
sort women surv79 faminc1_nm_prime
by women surv79: gen __rank1_prime = sum(weight) if  miss0==0 & miss1_prime==0
gen _rank1_prime = __rank1_prime/N_prime if  miss0==0 & miss1_prime==0
bys women surv79 faminc1_nm_prime: egen rank1_prime = mean(_rank1_prime) if  miss0==0 & miss1_prime==0
replace rank1_prime = 100*rank1_prime 



* --------- Rank-Rank --------- *


* Prime Earnings
reg rank1_prime perc66 perc79 surv79 women [w=weight] , cluster(hhid)
eststo rank_prime
test perc66=perc79
estadd scalar p = r(p): rank_prime
gen s=e(sample)
count if surv79==0 & s==1
estadd scalar N66 = r(N): rank_prime
count if surv79==1 & s==1
estadd scalar N79 = r(N): rank_prime
lincom perc79-perc66
estadd scalar diff = r(estimate): rank_prime
estadd scalar se = r(se): rank_prime
drop s


* Early Earnings
reg rank1_early perc66 perc79 surv79 women [w=weight] , cluster(hhid)
eststo rank_early
test perc79=perc66
estadd scalar p = r(p): rank_early
gen s=e(sample)
count if surv79==0 & s==1
estadd scalar N66 = r(N): rank_early
count if surv79==1 & s==1
estadd scalar N79 = r(N): rank_early
lincom perc79-perc66
estadd scalar diff = r(estimate): rank_early
estadd scalar se = r(se): rank_early
drop s



* --------- IGE --------- *


* Prime
reg lfaminc1_prime loginc66 loginc79 surv79  [w=weight], cluster(hhid)
eststo IGE_prime
test loginc79=loginc66
estadd scalar p = r(p): IGE_prime
gen s=e(sample)
count if surv79==0 & s==1
estadd scalar N66 = r(N): IGE_prime
count if surv79==1 & s==1
estadd scalar N79 = r(N): IGE_prime
lincom loginc79-loginc66
estadd scalar diff = r(estimate): IGE_prime
estadd scalar se = r(se): IGE_prime
drop s

* Early
reg lfaminc1_early loginc66 loginc79 surv79  [w=weight], cluster(hhid)
eststo IGE_early
test loginc79=loginc66
estadd scalar p = r(p): IGE_early
gen s=e(sample)
count if surv79==0 & s==1
estadd scalar N66 = r(N): IGE_early
count if surv79==1 & s==1
estadd scalar N79 = r(N): IGE_early
lincom loginc79-loginc66
estadd scalar diff = r(estimate): IGE_early
estadd scalar se = r(se): IGE_early
drop s

#delimit ;
estout rank_prime IGE_prime rank_early IGE_early
	using  "${results_dir}tableA8.txt", replace
	keep(perc66 perc79) 
	rename(loginc66 perc66 loginc79 perc79)
	cells(b(star fmt(3))  se(par fmt(3))) 
	collabels(,none) stat(diff se p  N66 N79) stardrop(*) ;	
#delimit cr	

